package Web;

import com.google.gson.Gson;
import service.UserService;
import service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.ref.ReferenceQueue;
import java.util.HashMap;

/**
 * @author jianjun
 * @Describe
 * @creat 2022-01-05-16:30
 */
public class AjaxServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    protected void existsUsername(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数username
        String username = request.getParameter("username");
        //调用existsUsername()方法
        boolean existsUsername = userService.existsUsername(username);
        //把返回结果封装成为map对象
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("existsUsername",existsUsername);
        Gson gson = new Gson();
        String json = gson.toJson(resultMap);

        response.getWriter().write(json);
    }
}
